package com.ruoyi.cqjd.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.cqjd.domain.CqjdCategory;
import com.ruoyi.cqjd.dto.CqjdDishDto;
import com.ruoyi.cqjd.service.ICqjdCategoryService;
import org.junit.experimental.categories.Category;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cqjd.domain.CqjdDish;
import com.ruoyi.cqjd.service.ICqjdDishService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 菜品管理Controller
 * 
 * @author ruoyi
 * @date 2022-06-20
 */
@RestController
@RequestMapping("/cqjd/cqjd_dish")
public class CqjdDishController extends BaseController
{
    @Autowired
    private ICqjdDishService cqjdDishService;

    @Autowired
    private ICqjdCategoryService cqjdCategoryService;



    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/front_list")
    public R<List<CqjdDishDto>> frontList(CqjdDish cqjdDish){

        System.out.println("cqjdDish.getCategoryId()="+cqjdDish.getCategoryId());

        List<CqjdDishDto> dishDtoList;
        String key="dish_"+cqjdDish.getCategoryId()+"_"+cqjdDish.getStatus();

//        先查询redis中是否有数据
        dishDtoList = (List<CqjdDishDto>) redisTemplate.opsForValue().get(key);
//        有数据就直接返回
        if (dishDtoList!=null){
            return R.ok(dishDtoList);
        }


        dishDtoList=new ArrayList<>();

        List<CqjdDish> list = cqjdDishService.getDishListByCategoryIdAndStatusIsOn(cqjdDish.getCategoryId());
        for (CqjdDish dish : list) {
            CqjdDishDto cqjdDishDto=new CqjdDishDto();
            BeanUtils.copyProperties(dish, cqjdDishDto);
            CqjdCategory cqjdCategory = cqjdCategoryService.selectCqjdCategoryById(dish.getCategoryId());
            if (cqjdCategory != null) {
                String categoryName = cqjdCategory.getName();
                cqjdDishDto.setCategoryName(categoryName);
            }
            dishDtoList.add(cqjdDishDto);
        }

//        没有就查数据，然后把数据缓存到redis中
        redisTemplate.opsForValue().set(key,dishDtoList,60, TimeUnit.MINUTES);

        return R.ok(dishDtoList);
    }


    /**
     * 查询菜品管理列表
     */
//    @PreAuthorize("@ss.hasPermi('cqjd:cqjd_dish:list')")
    @GetMapping("/list")
    public TableDataInfo list(CqjdDish cqjdDish)
    {
        startPage();
        List<CqjdDish> list = cqjdDishService.selectCqjdDishList(cqjdDish);
        List<CqjdDishDto> cqjdDishDtoList=new ArrayList<>();
        for (CqjdDish dish : list) {
            CqjdDishDto cqjdDishDto=new CqjdDishDto();
            BeanUtils.copyProperties(dish, cqjdDishDto);
            CqjdCategory cqjdCategory = cqjdCategoryService.selectCqjdCategoryById(dish.getCategoryId());
            if (cqjdCategory != null) {
                String categoryName = cqjdCategory.getName();
                cqjdDishDto.setCategoryName(categoryName);
            }
            cqjdDishDtoList.add(cqjdDishDto);
        }
        return getDataTable(cqjdDishDtoList);
    }

    /**
     * 导出菜品管理列表
     */
//    @PreAuthorize("@ss.hasPermi('cqjd:cqjd_dish:export')")
    @Log(title = "菜品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CqjdDish cqjdDish)
    {
        List<CqjdDish> list = cqjdDishService.selectCqjdDishList(cqjdDish);
        ExcelUtil<CqjdDish> util = new ExcelUtil<CqjdDish>(CqjdDish.class);
        util.exportExcel(response, list, "菜品管理数据");
    }

    /**
     * 获取菜品管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('cqjd:cqjd_dish:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cqjdDishService.selectCqjdDishById(id));
    }

    /**
     * 新增菜品管理
     */
//    @PreAuthorize("@ss.hasPermi('cqjd:cqjd_dish:add')")
    @Log(title = "菜品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CqjdDish cqjdDish)
    {
        return toAjax(cqjdDishService.insertCqjdDish(cqjdDish));
    }

    /**
     * 修改菜品管理
     */
//    @PreAuthorize("@ss.hasPermi('cqjd:cqjd_dish:edit')")
    @Log(title = "菜品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CqjdDish cqjdDish)
    {
        return toAjax(cqjdDishService.updateCqjdDish(cqjdDish));
    }

    /**
     * 删除菜品管理
     */
//    @PreAuthorize("@ss.hasPermi('cqjd:cqjd_dish:remove')")
    @Log(title = "菜品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cqjdDishService.deleteCqjdDishByIds(ids));
    }
}
